home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / jnos / jnos_src / pc.c < prev    next >
C/C++ Source or Header  |  1994-08-26  |  46KB  |  1,672 lines

  1. /* OS- and machine-dependent stuff for IBM-PC running MS-DOS and Turbo-C
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  *
  4.  * XMS extensions Copyright 1993 Johan. K. Reinalda, WG7J
  5.  */
  6. #include <dir.h>
  7. #include <dos.h>
  8. #include <io.h>
  9. #include <conio.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <process.h>
  13. #include <fcntl.h>
  14. #include <alloc.h>
  15. #include <stdarg.h>
  16. #include <bios.h>
  17. #include <time.h>
  18. #include "global.h"
  19. #include "mbuf.h"
  20. #include "proc.h"
  21. #include "iface.h"
  22. #include "internet.h"
  23. #include "session.h"
  24. #include "tty.h"
  25. #include "usock.h"
  26. #include "socket.h"
  27. #include "smtp.h"
  28. #include "cmdparse.h"
  29. #include "dirutil.h"
  30. #include "files.h"
  31. #include "pc.h"
  32. #include "index.h"
  33. #include "mailbox.h"
  34. #ifdef EMS
  35. #include "memlib.h"
  36. #endif
  37. #ifdef XMS
  38. #include "xms.h"
  39. #endif
  40.   
  41. #define CTLC    0x3
  42. #define DEL     0x7f
  43.   
  44. static int kbchar __ARGS((void));
  45. extern int Curdisp;
  46. extern struct proc *Display;
  47. FILE *Rawterm;
  48. /* Highest i've ever seen in 'ps' is around 400, ie 800 bytes - WG7J */
  49. unsigned _stklen = 2048;
  50. volatile int Tick;
  51. static int32 Starttime;
  52. int32 Clock;
  53.   
  54. #ifdef MULTITASK
  55. int Background;
  56. int Nokeys;
  57. extern unsigned Minheap;
  58. #endif
  59. extern int Tracesession;
  60.   
  61. #ifdef SPLITSCREEN
  62. extern char MainColors;
  63. extern char SplitColors;
  64. #endif
  65. #ifdef STATUSWIN
  66. extern int StatusLines;
  67. char MainStColors = WHITE+(MAGENTA<<4);
  68. char SesStColors = WHITE+(BLUE<<4);
  69. #else
  70. int StatusLines = 0;
  71. #endif
  72.   
  73. char *Screen;
  74. int ScreenSize;
  75. int SwapMode;
  76. #ifdef XMS
  77. unsigned int ScreenSizeK;
  78. #endif
  79.   
  80. int Watchdog;                       /* Watch Dog off by default */
  81. int WDTick = 300*(1000 / MSPTICK);      /* 5 minutes watchdog timer */
  82. int WDCurr = 300*(1000 / MSPTICK);      /* Initial count down timer */
  83.   
  84. /* This flag is set by setirq() if IRQ 8-15 is used, indicating
  85.  * that the machine is a PC/AT with a second 8259 interrupt controller.
  86.  * If this flag is set, the interrupt return code in pcgen.asm will
  87.  * send an End of Interrupt command to the second 8259 as well as the
  88.  * first.
  89.  */
  90. int Isat;
  91.   
  92. static char Ttbuf[BUFSIZ];
  93. static char Tsbuf[BUFSIZ];
  94. static int saved_break;
  95.   
  96. int am_i_an_AT __ARGS((void));
  97.   
  98.   
  99. /* Keyboard input buffer */
  100. #define KBSIZE  256
  101. static struct {
  102.     char buf[KBSIZE];
  103.     char *wp;
  104.     char *rp;
  105.     int cnt;
  106. } Keyboard;
  107.   
  108. #if !defined CPU286 && !defined CPU386 && !defined CPU486 && !defined CPU586
  109. int
  110. am_i_an_AT()
  111. {
  112.     unsigned char *model_code = MK_FP(0xF000,0xFFFE);
  113.   
  114.     if(*model_code == 0xFC)
  115.         return 1;
  116.     else
  117.         return 0;
  118. }
  119. #endif
  120.   
  121. /* Following code from Doug Crompton */
  122. /* define the error messages for trapping disk problems
  123.  */
  124. static char *crit_err_msg[] = {
  125.     "write protect",
  126.     "unknown unit",
  127.     "not ready",
  128.     "unknown command",
  129.     "data error (CRC)",
  130.     "bad request",
  131.     "seek error",
  132.     "unknown media type",
  133.     "sector not found",
  134.     "printer out of paper",
  135.     "write fault",
  136.     "read fault",
  137.     "general failure",
  138.     "reserved",
  139.     "reserved",
  140.     "invalid disk change"
  141. };
  142.   
  143.   
  144. int
  145. errhandler(int errval,int ax,int bp,int si)
  146. {
  147.     char msg[80];
  148.     unsigned di;
  149.     int drive;
  150.     int errorno;
  151.   
  152.     di= _DI;
  153.   
  154.     if (ax < 0)
  155.         hardretn(3);
  156.     drive = ax & 0x00FF;
  157.     errorno = di & 0x00FF;
  158.     sprintf(msg, "\r\nError: %s on drive %c\r\n$",
  159.     crit_err_msg[errorno], 'A' + drive);
  160.     bdosptr(0x09,msg,0);
  161.     hardretn(3);
  162.     return 0;   /* to please the compiler */
  163. }
  164.   
  165. int c_break(void) {     /* ctrl-brk or ctrl-c handler */
  166.     return 1;
  167. }
  168.   
  169. #ifdef __BORLANDC__
  170. #undef fopen
  171. FILE _FAR *_Cdecl fopen(const char _FAR *__path, const char _FAR *__mode);
  172. #endif
  173.   
  174. /* Called at startup time to set up console I/O, memory heap */
  175. void
  176. ioinit()
  177. {
  178.   
  179.     /* Fail all I/O errors */
  180.     harderr(errhandler);
  181.   
  182.     /* Save these two file table entries for something more useful */
  183.     fclose(stdaux);
  184.     fclose(stdprn);
  185.     setbuf(stdout,Tsbuf);
  186.   
  187.     Rawterm = fopen("con","wb");
  188.     setbuf(Rawterm,Ttbuf);
  189.     /* this breaks tab expansion so you must use ANSI or NANSI */
  190.     ioctl(fileno(Rawterm), 1, (ioctl(fileno(Rawterm),0) & 0xff) | 0x20);
  191.     saved_break = getcbrk();
  192.     setcbrk(0);
  193.     ctrlbrk(c_break);
  194.   
  195. #ifdef MSDOS
  196. #if !defined CPU286 && !defined CPU386 && !defined CPU486 && !defined CPU586
  197.     /* test to see if we're running on an AT class machine.
  198.      * Set Isat flag accordingly - N1BEE
  199.      */
  200.     Isat = am_i_an_AT();
  201. #endif
  202. #endif
  203.     Starttime = bioscnt();
  204.     /* Link timer handler into timer interrupt chain */
  205.     chtimer(btick);
  206.   
  207.     /* Find out what multitasker we're running under, if any */
  208.     chktasker();
  209.   
  210.     /* Initialize keyboard queue */
  211.     Keyboard.rp = Keyboard.wp = Keyboard.buf;
  212.   
  213.     /* Check for essential directories. */
  214.     mkdir(LogsDir);
  215.     mkdir(Fdir);
  216.     mkdir(Spoolqdir);
  217.     mkdir(Mailspool);
  218.     mkdir(Mailqdir);
  219.     mkdir(Routeqdir);
  220. #ifdef MAILBOX
  221.     mkdir(Helpdir);
  222.     mkdir(Signature);
  223. #endif
  224. #if defined NNTPS || defined NNTP
  225.     mkdir(Newsdir);
  226. #endif
  227. }
  228.   
  229. /* Called just before exiting to restore console state */
  230. void
  231. iostop()
  232. {
  233.     struct iface *ifp,*iftmp;
  234.     void (**fp)(void);
  235.   
  236.     setbuf(Rawterm,NULLCHAR);
  237.     ioctl(fileno(Rawterm), 1, ioctl(fileno(Rawterm), 0) & 0xff & ~0x20);
  238.     setcbrk(saved_break);
  239.   
  240.     for(ifp = Ifaces;ifp != NULLIF;ifp = iftmp){
  241.         iftmp = ifp->next;
  242.         if_detach(ifp);
  243.     }
  244.     /* Call list of shutdown functions */
  245.     for(fp = Shutdown;*fp != NULLVFP;fp++){
  246.         (**fp)();
  247.     }
  248. }
  249. #ifdef SHELL
  250. /* Spawn subshell */
  251. int
  252. doshell(argc,argv,p)
  253. int argc;
  254. char *argv[];
  255. void *p;
  256. {
  257.     char *command;
  258.     int ret;
  259.     int OldWd;
  260.   
  261. #ifdef MULTITASK
  262.     if(Background) {
  263.         if(!start_back())
  264.             return -1;
  265.         Nokeys++;
  266.         free(mallocw(Minheap)); /* Force heap/core break to reserve a heap */
  267.     } else
  268. #endif
  269.     {
  270.         OldWd = Watchdog;   /* Save old watchdog state, and turn it off */
  271.         Watchdog = 0;       /* while shelled out */
  272.     }
  273.   
  274.     if(argc == 1 || !stricmp(argv[1], "/c")) {
  275.         if((command = getenv("COMSPEC")) == NULLCHAR)
  276.             command = "COMMAND.COM";
  277.         ret = spawnvp(P_WAIT,command,argv);
  278.     } else {
  279.         ret = spawnvp(P_WAIT,argv[1],(argv + 1));
  280.     }
  281.   
  282. #ifdef MULTITASK
  283.     if(Background) {
  284.         Nokeys--;
  285.         stop_back();
  286.     } else
  287. #endif
  288.     {
  289.         Watchdog = OldWd;   /* Restore old watchdog state, */
  290.         WDCurr = WDTick;    /* and start with a fresh count */
  291.     }
  292.   
  293.     /* Update index files - WG7J */
  294.     UpdateIndex(NULL,0);
  295.   
  296.     return ret;
  297. }
  298. #endif
  299.   
  300. #ifdef ALLCMD
  301. /* Spawn mailer as subshell */
  302. int
  303. dobmail(argc,argv,p)
  304. int argc;
  305. char *argv[];
  306. void *p;
  307. {
  308.     char *command;
  309.     int ret;
  310.     int OldWd;
  311.   
  312. #ifdef MULTITASK
  313.     if(Background) {
  314.         if(!start_back())
  315.             return -1;
  316.         Nokeys++;
  317.         free(mallocw(Minheap));
  318.     } else
  319. #endif
  320.     {
  321.         OldWd = Watchdog;   /* Save old watchdog state, and turn it off */
  322.         Watchdog = 0;       /* while shelled out */
  323.     }
  324.   
  325.     if((command = getenv("MAILER")) == NULLCHAR)
  326.         command = "BM.EXE";
  327.     ret = spawnvp(P_WAIT,command,argv);
  328.   
  329. #ifdef MULTITASK
  330.     if(Background) {
  331.         Nokeys--;
  332.         stop_back();
  333.     } else
  334. #endif
  335.     {
  336.         Watchdog = OldWd;   /* Restore old watchdog state, */
  337.         WDCurr = WDTick;    /* and start with a fresh count */
  338.     }
  339.   
  340.     /* Update index files - WG7J */
  341.     UpdateIndex(NULL,0);
  342.   
  343.     smtptick(NULL);         /* tickle smtp to send any mail */
  344.     return ret;
  345. }
  346. #endif /*ALLCMD*/
  347.   
  348. #ifdef MULTITASK
  349. /* if multitask mode is set - allow NOS and shell/mail to share system time */
  350. int dobackg(argc,argv,p)
  351. int argc;
  352. char *argv[];
  353. void *p;
  354. {
  355.     return setbool(&Background,"Multitasking DOS Shell ",argc,argv);
  356. }
  357. #endif
  358.   
  359. /* if watch-dog mode is set - make NOS reboot the system if it stalls */
  360. int dowatchdog(argc,argv,p)
  361. int argc;
  362. char *argv[];
  363. void *p;
  364. {
  365.     return setbool(&Watchdog,"NOS Watch Dog",argc,argv);
  366. }
  367.   
  368. /* Keyboard interrupt handler */
  369. void
  370. kbint()
  371. {
  372.     int sig = 0;
  373.     int c;
  374.   
  375. #ifdef MULTITASK
  376.     if(Background && Nokeys)
  377.         return;
  378. #endif
  379.   
  380.     while((c = kbraw()) != -1 && Keyboard.cnt < KBSIZE){
  381.         sig = 1;
  382.         *Keyboard.wp++ = c;
  383.         if(Keyboard.wp == &Keyboard.buf[KBSIZE])
  384.             Keyboard.wp = Keyboard.buf;
  385.         Keyboard.cnt++;
  386.     }
  387.     if(sig){
  388.         psignal(&Keyboard,0);
  389.     }
  390. }
  391. static int
  392. kbchar()
  393. {
  394.     int i_state;
  395.     char c;
  396.   
  397. #ifdef MULTITASK
  398.     if(Background && Nokeys)
  399.         return -1;
  400. #endif
  401.   
  402.     i_state = dirps();
  403.     while(Keyboard.cnt == 0)
  404.         pwait(&Keyboard);
  405.     Keyboard.cnt--;
  406.     restore(i_state);
  407.     c = *Keyboard.rp++;
  408.     if(Keyboard.rp == &Keyboard.buf[KBSIZE])
  409.         Keyboard.rp = Keyboard.buf;
  410.     return uchar(c);
  411. }
  412. /* Flush the raw terminal output */
  413. void
  414. rflush()
  415. {
  416.     fflush(Rawterm);
  417. }
  418.   
  419. #ifdef MSDOS
  420. #ifdef ALLCMD
  421. struct funcstr {
  422.     int fkey;
  423.     char alloced;
  424.     char *name;
  425.     char *fvalue;
  426. };
  427.   
  428. static struct funcstr DFAR fkeys[] = {
  429.     15,0,"Stab",NULLCHAR,  /* tab + shift */
  430.     59,0,"F1",NULLCHAR,  /* F1 */
  431.     60,0,"F2",NULLCHAR,  /* F2 */
  432.     61,0,"F3",NULLCHAR,  /* F3 */
  433.     62,0,"F4",NULLCHAR,  /* F4 */
  434.     63,0,"F5",NULLCHAR,  /* F5 */
  435.     64,0,"F6",NULLCHAR,  /* F6 */
  436.     65,0,"F7",NULLCHAR,  /* F7 */
  437.     66,0,"F8",NULLCHAR,  /* F8 */
  438.     67,0,"F9",NULLCHAR,  /* F9 */
  439.     68,0,"F10",NULLCHAR,  /* F10 */
  440.     71,1,"home","\010",    /* home*/
  441.     72,1,"up","\033[A",  /* up arrow*/
  442.     73,1,"pgup","\025",    /* pgup */
  443.     75,1,"left","\033[D",  /* left arrow */
  444.     77,1,"right","\033[C",  /* right arrow */
  445.     79,1,"end","\005",    /* end */
  446.     80,1,"down","\033[B",  /* down arrow */
  447.     81,1,"pgdn","\012",    /* pgdn */
  448.     82,1,"ins","\001",    /* ins */
  449.     83,1,"del","\177",   /* del */
  450.     84,0,"SF1",NULLCHAR,  /* F1 + shift*/
  451.     85,0,"SF2",NULLCHAR,  /* F2 + shift*/
  452.     86,0,"SF3",NULLCHAR,  /* F3 + shift*/
  453.     87,0,"SF4",NULLCHAR,  /* F4 + shift*/
  454.     88,0,"SF5",NULLCHAR,  /* F5 + shift*/
  455.     89,0,"SF6",NULLCHAR,  /* F6 + shift*/
  456.     90,0,"SF7",NULLCHAR,  /* F7 + shift*/
  457.     91,0,"SF8",NULLCHAR,  /* F8 + shift*/
  458.     92,0,"SF9",NULLCHAR,  /* F9 + shift*/
  459.     93,0,"SF10",NULLCHAR,  /* F10 + shift*/
  460.     94,0,"CF1",NULLCHAR,  /* F1 + control*/
  461.     95,0,"CF2",NULLCHAR,  /* F2 + control*/
  462.     96,0,"CF3",NULLCHAR,  /* F3 + control*/
  463.     97,0,"CF4",NULLCHAR,  /* F4 + control*/
  464.     98,0,"CF5",NULLCHAR,  /* F5 + control*/
  465.     99,0,"CF6",NULLCHAR,  /* F6 + control*/
  466.     100,0,"CF7",NULLCHAR, /* F7 + control*/
  467.     101,0,"CF8",NULLCHAR, /* F8 + control*/
  468.     102,0,"CF9",NULLCHAR, /* F9 + control*/
  469.     103,0,"CF10",NULLCHAR, /* F10 + control*/
  470.     104,0,"AF1",NULLCHAR, /* F1 + alt*/
  471.     105,0,"AF2",NULLCHAR, /* F2 + alt*/
  472.     106,0,"AF3",NULLCHAR, /* F3 + alt*/
  473.     107,0,"AF4",NULLCHAR, /* F4 + alt*/
  474.     108,0,"AF5",NULLCHAR, /* F5 + alt*/
  475.     109,0,"AF6",NULLCHAR, /* F6 + alt*/
  476.     110,0,"AF7",NULLCHAR, /* F7 + alt*/
  477.     111,0,"AF8",NULLCHAR, /* F8 + alt*/
  478.     112,0,"AF9",NULLCHAR, /* F9 + alt*/
  479.     113,0,"AF10",NULLCHAR, /* F10 + alt*/
  480.     114,0,"Cprnt",NULLCHAR, /* PrtSc + ctl*/
  481.     117,0,"Cend",NULLCHAR, /* end  + ctl */
  482.     118,0,"Cpgup",NULLCHAR, /* pgup + ctl */
  483.     119,0,"Chome",NULLCHAR, /* home + ctl */
  484.     132,0,"Cpgdn",NULLCHAR, /* pgdn + ctl */
  485.     133,0,"F11",NULLCHAR, /* F11 */
  486.     134,0,"F12",NULLCHAR, /* F12 */
  487.     135,0,"SF11",NULLCHAR, /* F11 + shift */
  488.     136,0,"SF12",NULLCHAR, /* F12 + shift */
  489.     137,0,"CF11",NULLCHAR, /* F11 + ctrl */
  490.     138,0,"CF12",NULLCHAR, /* F12 + ctrl */
  491.     139,0,"AF11",NULLCHAR, /* F11 + alt */
  492.     140,0,"AF12",NULLCHAR, /* F12 + alt */
  493.     0,0,NULL,NULLCHAR
  494. };
  495.   
  496. char Leftover = 0;
  497. char *Nextkey;
  498. #endif /*ALLCMD*/
  499. #endif /*MSDOS*/
  500.   
  501. /* Read characters from the keyboard, translating them to "real" ASCII.
  502.  * If none are ready, block. The F-10 key is special; translate it to -2.
  503.  * Modified for function key session switching, and command recall - WG7J
  504.  */
  505. #ifdef ALLCMD
  506. int
  507. kbread()
  508. {
  509. #ifndef MSDOS
  510.     int c;
  511. #else
  512.     int c,i,j;
  513.   
  514.     if((c = Leftover) != 0)  {
  515.         Leftover = *Nextkey++;
  516.         return c;
  517.     }
  518. #endif  MSDOS
  519.     if((c = kbchar()) == 0){
  520.         /* Lead-in to a special char */
  521.         c = kbchar();
  522.         if(Current == Command) {    /* Check for command recall */
  523.             if(c == 72)     /* UP arrow */
  524.                 return UPARROW;
  525.             if(c == 80)     /* DOWN arrow */
  526.                 return DNARROW;
  527.         }
  528.         switch(c){
  529.             case 3:         /* NULL (bizzare!) */
  530.                 c = 0;
  531.                 break;
  532.             case 68:    /* F-10 key (used as command-mode escape) */
  533.                 if(fkeys[10].fvalue == NULLCHAR){
  534.                     c = -2;
  535.                     break;
  536.                 }
  537.             default:    /* Dunno what it is */
  538. #ifdef  MSDOS
  539.                 if(c > 58 && c < 68) {  /* F1 to F9 */
  540.                     if(fkeys[c-58].fvalue == NULLCHAR) {
  541.                         c = (c - 56) * -1; /* NO fkey defined - WG7J */
  542.                         break;
  543.                     }
  544.                 }
  545.                 for(i=0;(j = fkeys[i].fkey) != 0;i++)
  546.                     if(j == c) {
  547.                         Nextkey = fkeys[i].fvalue;
  548.                         if(Nextkey == NULLCHAR) {
  549.                             c = -1;
  550.                             return c;
  551.                         }
  552.             /* If first char of fvalue is '~'
  553.              * switch to command session.
  554.              */
  555.                         if((c = *Nextkey++) == '~') {
  556.                             c = -2;
  557.                             Leftover = *Nextkey++;
  558.                         } else {
  559.                             if(c != 0)
  560.                                 Leftover = *Nextkey++;
  561. #ifdef notdef
  562.                             if (c == '~') /* switch to command ses. on ~ char */
  563.                                 c = -2;  /* KN4L Change */
  564. #endif
  565.                             else
  566.                                 c = -1;
  567.                         }
  568.                         return c;
  569.                     }
  570. #endif
  571.                 c = -1;
  572.         }
  573.     }
  574.     return c;
  575. }
  576. #else /*ALLCMD*/
  577.   
  578. int
  579. kbread()
  580. {
  581.     int c;
  582.   
  583.     if((c = kbchar()) == 0){
  584.         /* Lead-in to a special char */
  585.         c = kbchar();
  586.         if(Current == Command) {    /* Check for command recall */
  587.             if(c == 72)     /* UP arrow */
  588.                 return UPARROW;
  589.             if(c == 80)     /* DOWN arrow */
  590.                 return DNARROW;
  591.         }
  592.         switch(c){
  593.             case 3:         /* NULL (bizzare!) */
  594.                 c = 0;
  595.                 break;
  596.             case 68:        /* F-10 key (used as command-mode escape) */
  597.                 c = -2;
  598.                 break;
  599.             case 83:        /* DEL key */
  600.                 c = 0x7f;
  601.                 break;
  602.             default:        /* Dunno what it is */
  603.                 if(c > 58 && c < 68)    /* F1 to F9 */
  604.                     c = (c - 56) * -1;
  605.                 else
  606.                     c = -1;
  607.         }
  608.     }
  609.     return c;
  610. }
  611.   
  612. #endif /*ALLCMD*/
  613.   
  614. #ifdef  MSDOS
  615. #ifdef ALLCMD
  616. int
  617. dofkey(argc,argv,p)
  618. int argc;
  619. char *argv[];
  620. void *p;
  621. {
  622.     int c,i,j;
  623.     char *q, *r;
  624.     char str[100];
  625.   
  626.     if(argc == 1) {
  627.         tputs("\n  key num definition             key num definition\n");
  628.         for(i=0;fkeys[i].fkey != 0;i++) {
  629.             char *s;
  630.             if((s=fkeys[i].fvalue) == NULL)
  631.                 s = "";
  632.             else {
  633.                 q=s;
  634.                 r=str;
  635.                 s=str;
  636.                 while(*q)
  637.                     if(*q < ' ') { /* This is ASCII dependent !! */
  638.                         *r++ = '^';
  639.                         *r++ = *q++ | 0x40;
  640.                     } else if (*q == '^') {
  641.                         *r++ = '^';
  642.                         *r++ = *q++;
  643.                     } else if (*q == '\177') {
  644.                         *r++ = '^';
  645.                         *r++ = '?';
  646.                         q++;
  647.                     } else {
  648.                         *r++ = *q++;
  649.                     }
  650.                 *r = '\0';
  651.             }
  652.             tprintf("%5.5s %3d %-20.20s ",fkeys[i].name,fkeys[i].fkey,s);
  653.             if(i%2)
  654.                 tputc('\n');
  655.         }
  656.         tputs("\nusage: fkey <key number> [<value> | \"string\"]\n");
  657.         return 0;
  658.     }
  659.   
  660.     c = atoi(argv[1]);
  661.     if(c == 0 || c > 255) {
  662.         tputs("fkey number out of range.\n");
  663.         return 1;
  664.     }
  665.   
  666.     for(j = 0;(i = fkeys[j].fkey) != 0; j++)
  667.         if(i == c)
  668.             break;
  669.   
  670.     if(i == 0){
  671.         tputs("fkey number not found\n");
  672.         return 1;
  673.     }
  674.   
  675.     if(argc == 2) {
  676.         q = fkeys[j].fvalue;
  677.         r = str;
  678.         if(q == NULLCHAR)
  679.             tprintf("fkey %d has no assigned value.\n",c);
  680.         else {
  681.             while(*q)
  682.                 if(*q < ' ') { /* This is ASCII dependent !! */
  683.                     *r++ = '^';
  684.                     *r++ = *q++ + 0x40;
  685.                 } else if (*q == '^') {
  686.                     *r++ = '^';
  687.                     *r++ = *q++;
  688.                 } else if (*q == '\177') {
  689.                     *r++ = '^';
  690.                     *r++ = '?';
  691.                     q++;
  692.                 } else
  693.                     *r++ = *q++;
  694.             *r = '\0';
  695.             tprintf("fkey = %s\n",str);
  696.         }
  697.         return 0;
  698.     }
  699.   
  700.     if(argc == 3) {
  701.         if(fkeys[j].alloced)
  702.             fkeys[j].alloced = 0;
  703.         else
  704.             if(fkeys[j].fvalue != NULLCHAR)
  705.                 free(fkeys[j].fvalue);
  706.   
  707.         r = str;
  708.         q = argv[2];
  709.         while(*q){
  710.             if(*q == '^'){  /* ^ gives control char next */
  711.                 q++;
  712.                 if(*q == '^') {
  713.                     *r++ = *q++; /* No, he wants a ^ */
  714.                 } else if (*q == '?') {
  715.                     *r++ = '\177';
  716.                     q++;
  717.                 } else {
  718.                     *r++ = *q++ & 0x1f;
  719.                 }
  720.             } else
  721.                 *r++ = *q++;
  722.         }
  723.         *r = '\0';
  724.         fkeys[j].fvalue = strdup(str);
  725.     }
  726.     return 0;
  727. }
  728. #endif /*ALLCMD*/
  729. #endif /*MSDOS*/
  730.   
  731. /* Install hardware interrupt handler.
  732.  * Takes IRQ numbers from 0-7 (0-15 on AT) and maps to actual 8086/286 vectors
  733.  * Note that bus line IRQ2 maps to IRQ9 on the AT
  734.  */
  735. int
  736. setirq(irq,handler)
  737. unsigned irq;
  738. INTERRUPT (*handler)();
  739. {
  740.     /* Set interrupt vector */
  741.     if(irq < 8){
  742.         setvect(8+irq,handler);
  743.     } else if(irq < 16){
  744.         Isat = 1;
  745.         setvect(0x70 + irq - 8,handler);
  746.     } else {
  747.         return -1;
  748.     }
  749.     return 0;
  750. }
  751.   
  752. /* Return pointer to hardware interrupt handler.
  753.  * Takes IRQ numbers from 0-7 (0-15 on AT) and maps to actual 8086/286 vectors
  754.  */
  755. INTERRUPT
  756. (*getirq(irq))()
  757. unsigned int irq;
  758. {
  759.     /* Set interrupt vector */
  760.     if(irq < 8){
  761.         return getvect(8+irq);
  762.     } else if(irq < 16){
  763.         return getvect(0x70 + irq - 8);
  764.     } else {
  765.         return NULLVIFP;
  766.     }
  767. }
  768. /* Disable hardware interrupt */
  769. int
  770. maskoff(irq)
  771. unsigned irq;
  772. {
  773.     if(irq < 8){
  774.         setbit(0x21,(char)(1<<irq));
  775.     } else if(irq < 16){
  776.         irq -= 8;
  777.         setbit(0xa1,(char)(1<<irq));
  778.     } else {
  779.         return -1;
  780.     }
  781.     return 0;
  782. }
  783. /* Enable hardware interrupt */
  784. int
  785. maskon(irq)
  786. unsigned irq;
  787. {
  788.     if(irq < 8){
  789.         clrbit(0x21,(char)(1<<irq));
  790.     } else if(irq < 16){
  791.         irq -= 8;
  792.         clrbit(0xa1,(char)(1<<irq));
  793.     } else {
  794.         return -1;
  795.     }
  796.     return 0;
  797. }
  798. /* Return 1 if specified interrupt is enabled, 0 if not, -1 if invalid */
  799. int
  800. getmask(irq)
  801. unsigned irq;
  802. {
  803.     if(irq < 8)
  804.         return (inportb(0x21) & (1 << irq)) ? 0 : 1;
  805.     else if(irq < 16){
  806.         irq -= 8;
  807.         return (inportb(0xa1) & (1 << irq)) ? 0 : 1;
  808.     } else
  809.         return -1;
  810. }
  811. /* Called from assembler stub linked to BIOS interrupt 1C, called on each
  812.  * hardware clock tick. Signal a clock tick to the timer process.
  813.  */
  814. void
  815. ctick()
  816. {
  817.     if(Watchdog)
  818.         if(WDCurr-- == 0)
  819.             sysreset();
  820.     Tick++;
  821. #ifdef notdef
  822.     Clock++;        /* Keep system time */
  823. #endif
  824.     psignal(&Tick,1);
  825. }
  826. /* Called from the timer process on every tick. NOTE! This function
  827.  * can NOT be called at interrupt time because it calls the BIOS
  828.  */
  829. void
  830. pctick()
  831. {
  832.     long t;
  833.     static long oldt;       /* Value of bioscnt() on last call */
  834.     static long days;       /* # of times bioscnt() has rolled over */
  835.   
  836.     /* Update the time-since-boot */
  837.     t = bioscnt();
  838.   
  839.     if(t < oldt)
  840.         days++; /* bioscnt has rolled past midnight */
  841.     oldt = t;
  842.     Clock = (int32)((days * 0x1800b0L) + t - Starttime);
  843. }
  844.   
  845. /* Set bit(s) in I/O port */
  846. void
  847. setbit(port,bits)
  848. unsigned port;
  849. char bits;
  850. {
  851.     outportb(port,(char)inportb(port)|bits);
  852. }
  853. /* Clear bit(s) in I/O port */
  854. void
  855. clrbit(port,bits)
  856. unsigned port;
  857. char bits;
  858. {
  859.     outportb(port,(char)(inportb(port) & ~bits));
  860. }
  861. /* Set or clear selected bit(s) in I/O port */
  862. void
  863. writebit(port,mask,val)
  864. unsigned port;
  865. char mask;
  866. int val;
  867. {
  868.     register char x;
  869.   
  870.     x = inportb(port);
  871.     if(val)
  872.         x |= mask;
  873.     else
  874.         x &= ~mask;
  875.     outportb(port,x);
  876. }
  877. /* Convert a pointer to a long integer */
  878. long
  879. ptol(p)
  880. void *p;
  881. {
  882.     long x;
  883.   
  884.     x = FP_OFF(p);
  885. #ifdef  LARGEDATA
  886.     x |= (long)FP_SEG(p) << 16;
  887. #endif
  888.     return x;
  889. }
  890. void *
  891. ltop(l)
  892. long l;
  893. {
  894.     register unsigned int seg,offset;
  895.   
  896.     seg = (unsigned int)(l >> 16);
  897.     offset = (unsigned int)l;
  898.     return MK_FP(seg,offset);
  899. }
  900. #ifdef notdef   /* Assembler versions in pcgen.asm */
  901. /* Multiply a 16-bit multiplier by an arbitrary length multiplicand.
  902.  * Product is left in place of the multiplicand, and the carry is
  903.  * returned
  904.  */
  905. int16
  906. longmul(multiplier,n,multiplicand)
  907. int16 multiplier;
  908. int n;                          /* Number of words in multiplicand[] */
  909. register int16 *multiplicand;   /* High word is in multiplicand[0] */
  910. {
  911.     register int i;
  912.     unsigned long pc;
  913.     int16 carry;
  914.   
  915.     carry = 0;
  916.     multiplicand += n;
  917.     for(i=n;i != 0;i--){
  918.         multiplicand--;
  919.         pc = carry + (unsigned long)multiplier * *multiplicand;
  920.         *multiplicand = pc;
  921.         carry = pc >> 16;
  922.     }
  923. }
  924. return carry;
  925. }
  926. /* Divide a 16-bit divisor into an arbitrary length dividend using
  927.  * long division. The quotient is returned in place of the dividend,
  928.  * and the function returns the remainder.
  929.  */
  930. int16
  931. longdiv(divisor,n,dividend)
  932. int16 divisor;
  933. int n;                          /* Number of words in dividend[] */
  934. register int16 *dividend;       /* High word is in dividend[0] */
  935. {
  936.     /* Before each division, remquot contains the 32-bit dividend for this
  937.      * step, consisting of the 16-bit remainder from the previous division
  938.      * in the high word plus the current 16-bit dividend word in the low
  939.      * word.
  940.      *
  941.      * Immediately after the division, remquot contains the quotient
  942.      * in the low word and the remainder in the high word (which is
  943.      * exactly where we need it for the next division).
  944.      */
  945.     unsigned long remquot;
  946.     register int i;
  947.   
  948.     if(divisor == 0)
  949.         return 0;       /* Avoid divide-by-zero crash */
  950.     remquot = 0;
  951.     for(i=0;i<n;i++,dividend++){
  952.         remquot |= *dividend;
  953.         if(remquot == 0)
  954.             continue;       /* Avoid unnecessary division */
  955. #ifdef  __TURBOC__
  956.         /* Use assembly lang routine that returns both quotient
  957.          * and remainder, avoiding a second costly division
  958.          */
  959.         remquot = divrem(remquot,divisor);
  960.         *dividend = remquot;    /* Extract quotient in low word */
  961.         remquot &= ~0xffffL;    /* ... and mask it off */
  962. #else
  963.         *dividend = remquot / divisor;
  964.         remquot = (remquot % divisor) << 16;
  965. #endif
  966.     }
  967.     return remquot >> 16;
  968. }
  969. #endif
  970.   
  971. void
  972. sysreset()
  973. {
  974.     void (*foo) __ARGS((void));
  975.   
  976.     foo = MK_FP(0xffff,0);  /* FFFF:0000 is hardware reset vector */
  977.     (*foo)();
  978. }
  979.   
  980. void
  981. newscreen(sp)
  982. struct session *sp;
  983. {
  984.     if(sp != NULLSESSION)
  985.         sp->screen = callocw(1,sizeof(struct screen));
  986. }
  987.   
  988. void
  989. freescreen(sp)
  990. struct session *sp;
  991. {
  992.     if(sp == NULLSESSION || sp->screen == NULLSCREEN)
  993.         return;
  994. #ifdef XMS
  995.     if(sp->screen->stype == EMS_SWAP)
  996.         effree(sp->screen->sv.ems.token);
  997.     else
  998. #endif
  999. #ifdef XMS
  1000.         if(sp->screen->stype == XMS_SWAP && sp->screen->sv.handle != 0)
  1001.             Free_XMS(sp->screen->sv.handle);
  1002.         else
  1003. #endif
  1004.             if(sp->screen->stype == FILE_SWAP && sp->screen->sv.fp != NULL)
  1005.                 fclose(sp->screen->sv.fp);
  1006.             else if(sp->screen->stype == MEM_SWAP && sp->screen->sv.save != NULLCHAR)
  1007.                 free(sp->screen->sv.save);
  1008.     free((char *)sp->screen);
  1009. }
  1010.   
  1011. #ifdef XMS
  1012. /* Free XMS memory used for screen swapping.
  1013.  * Called right before exit().
  1014.  */
  1015. void Free_Screen_XMS(void) {
  1016.     struct session *sp;
  1017.     int i;
  1018.   
  1019.     for(i=0,sp=Sessions;i < Nsessions;sp++,i++)
  1020.         if(sp->type != FREE && sp->screen->stype == XMS_SWAP && \
  1021.             sp->screen->sv.handle != 0)
  1022.             Free_XMS(sp->screen->sv.handle);
  1023. }
  1024. #endif
  1025.   
  1026. extern int Numrows,Numcols;
  1027.   
  1028. /* Save specified session screen and resume console screen */
  1029. void
  1030. swapscreen(old,new)
  1031. struct session *old,*new;
  1032. {
  1033.     struct text_info tr;
  1034. #ifdef XMS
  1035.     long handle;
  1036.     struct XMS_Move X;
  1037. #endif
  1038.   
  1039.     if(old == new)
  1040.         return; /* Nothing to do */
  1041.   
  1042.     fflush(Rawterm);
  1043.     gettextinfo(&tr);
  1044.   
  1045.     if(old != NULLSESSION){
  1046. #ifdef SPLITSCREEN
  1047.         /* Save old screen */
  1048.         if(old->split){
  1049. //            window(1,1,Numcols,Numrows);
  1050.         }
  1051. #endif
  1052. #ifdef EMS
  1053.         if(old->screen->stype == EMS_SWAP) {
  1054.             set1eptr(old->screen->sv.ems.token,(void far**)&old->screen->sv.ems.save);
  1055.             if(old->screen->sv.ems.save != NULLCHAR)
  1056.                 memcpy(old->screen->sv.ems.save,Screen,ScreenSize);
  1057.         } else
  1058. #endif
  1059. #ifdef XMS
  1060.             if(old->screen->stype == XMS_SWAP) {
  1061.                 if(old->screen->sv.handle == 0)
  1062.                     old->screen->sv.handle = Alloc_XMS(ScreenSizeK);
  1063.                 if(old->screen->sv.handle != 0) {
  1064.                 /* Now transfer to XMS */
  1065.                     X.Length = (long) ScreenSize;
  1066.                     X.SourceHandle = 0; /* Indicate conventional memory */
  1067.                     X.SourceOffset = (long) Screen;
  1068.                     X.DestHandle = old->screen->sv.handle;
  1069.                     X.DestOffset = 0L;
  1070.                     Move_XMS(&X);
  1071.                 }
  1072.             } else
  1073. #endif
  1074.                 if(old->screen->stype == FILE_SWAP) {
  1075.                     if(old->screen->sv.fp == NULL)
  1076.                         old->screen->sv.fp = tmpfile();
  1077.                     else
  1078.                         fseek(old->screen->sv.fp,0L,0);
  1079.                     if(old->screen->sv.fp != NULL){
  1080.                         fwrite(Screen,ScreenSize,1,old->screen->sv.fp);
  1081.                     }
  1082.                 } else {
  1083.                     if(old->screen->sv.save == NULLCHAR)
  1084.                         old->screen->sv.save = malloc(ScreenSize);
  1085.                     if(old->screen->sv.save != NULLCHAR){
  1086.                         memcpy(old->screen->sv.save,Screen,ScreenSize);
  1087.                     }
  1088.                 }
  1089.         old->screen->row = tr.cury;
  1090.         old->screen->col = tr.curx;
  1091.     }
  1092.     if(new != NULLSESSION){
  1093.         /* Load new screen */
  1094.         if(new->screen->stype == UNKNOWN_SWAP) {
  1095.             /* A brand new screen */
  1096. #ifdef EMS
  1097.             if(SwapMode == EMS_SWAP) {
  1098.                 if(efmalloc(ScreenSize,&new->screen->sv.ems.token) != 0)
  1099.                     new->screen->sv.ems.token = -1;
  1100.                 new->screen->stype = EMS_SWAP;
  1101.             } else
  1102. #endif
  1103. #ifdef XMS
  1104.                 if(SwapMode == XMS_SWAP) {
  1105.                     new->screen->sv.handle = Alloc_XMS(ScreenSizeK);
  1106.                     new->screen->stype = XMS_SWAP;
  1107.                 } else
  1108. #endif
  1109.                     if(SwapMode == FILE_SWAP)
  1110.                         new->screen->stype = FILE_SWAP;
  1111.                     else
  1112.                         new->screen->stype = MEM_SWAP;
  1113.   
  1114. #ifdef SPLITSCREEN
  1115.             if(new->split){
  1116.                 new->tsavex = 1;
  1117.                 new->tsavey = 1;
  1118.                 new->bsavex = 1;
  1119.                 new->bsavey = Numrows-1;
  1120.                 /* The output window */
  1121.                 window(1,1+StatusLines,Numcols,Numrows);
  1122.                 textattr(SplitColors);
  1123.                 clrscr();       /* Start with a fresh slate */
  1124.                 cputs("_\b");
  1125.   
  1126.                 /* The input window */
  1127.                 window(1,1+StatusLines,Numcols,Numrows-2);
  1128.                 textattr(MainColors);
  1129.                 clrscr();       /* Start with a fresh slate */
  1130.                 cputs("_\b");
  1131.             } else
  1132. #endif
  1133.             {
  1134.                 /* leave room for the status line */
  1135.                 window(1,1+StatusLines,Numcols,Numrows);
  1136.                 clrscr();       /* Start with a fresh slate */
  1137.             }
  1138.         } else {
  1139. #ifdef SPLITSCREEN
  1140.             if(new->split){
  1141.                 window(1,1+StatusLines,Numcols,Numrows-2);
  1142.                 textattr(MainColors);
  1143.             } else
  1144. #endif
  1145.             {
  1146.                 window(1,1+StatusLines,Numcols,Numrows);
  1147.             }
  1148. #ifdef EMS
  1149.             if(new->screen->stype == EMS_SWAP &&
  1150.             (int)new->screen->sv.ems.token != -1) {
  1151.                 /* Get the text from EMS into screen buffer */
  1152.                 set1eptr(new->screen->sv.ems.token,(void far**)&new->screen->sv.ems.save);
  1153.                 memcpy(Screen,new->screen->sv.ems.save,ScreenSize);
  1154.             } else
  1155. #endif
  1156. #ifdef XMS
  1157.                 if(new->screen->stype == XMS_SWAP &&
  1158.                 new->screen->sv.handle) {
  1159.                 /* Get the text from XMS into screen buffer */
  1160.                     X.Length = (long) ScreenSize;
  1161.                     X.SourceHandle = new->screen->sv.handle;
  1162.                     X.SourceOffset = 0L;
  1163.                     X.DestHandle = 0L;  /* Indicate conventional memory */
  1164.                     X.DestOffset = (long) Screen;
  1165.                     Move_XMS(&X);
  1166.                 } else
  1167. #endif
  1168.                     if(new->screen->stype == FILE_SWAP &&
  1169.                     new->screen->sv.fp) {
  1170.                         fseek(new->screen->sv.fp,0L,0);
  1171.                         fread(Screen,ScreenSize,1,new->screen->sv.fp);
  1172.   
  1173.                 /* Not closing is a little faster,
  1174.                  * but takes more resources
  1175.                  * while the session is active - WG7J
  1176.                  */
  1177.                         fclose(new->screen->sv.fp);
  1178.                         new->screen->sv.fp = NULLFILE;
  1179.   
  1180.                     } else if(new->screen->stype == MEM_SWAP &&
  1181.                     new->screen->sv.save) {
  1182.                         memcpy(Screen,new->screen->sv.save,ScreenSize);
  1183.                     /* Free the memory (saves 4K on a continuous basis) */
  1184.                         free(new->screen->sv.save);
  1185.                         new->screen->sv.save = NULLCHAR;
  1186.                     } else
  1187.                 /* Somehow, we couldn't save the old screen */
  1188.                         clrscr();
  1189.             gotoxy(new->screen->col,new->screen->row);
  1190.         }
  1191.     }
  1192.     alert(Display,1);       /* Wake him up */
  1193. }
  1194.   
  1195. void
  1196. display(i,v1,v2)
  1197. int i;
  1198. void *v1;
  1199. void *v2;
  1200. {
  1201.     int c;
  1202.     struct session *sp;
  1203.   
  1204.     /* This is very tricky code. Because the value of "Current" can
  1205.      * change any time we do a pwait, we have to be careful to detect
  1206.      * any change and go back and start again.
  1207.      */
  1208.     for(;;){
  1209.         sp = Current;
  1210.   
  1211.         if(sp->morewait){
  1212.             pwait(&sp->row);
  1213.             if(sp != Current || sp->row <= 0){
  1214.                 /* Current changed value, or the user
  1215.                  * hasn't really hit a key
  1216.                  */
  1217.                 continue;
  1218.             }
  1219.             /* Erase the prompt */
  1220.             if(StatusLines)
  1221.                 cputs("\r        \r");
  1222.             else
  1223.                 fprintf(Rawterm,"\r        \r");
  1224.         }
  1225.         sp->morewait = 0;
  1226.         if((c = rrecvchar(sp->output)) == -1){
  1227.             /* the alert() in swapscreen will cause this to
  1228.              * return -1 when current changes
  1229.              */
  1230.             pwait(NULL);    /* Prevent a nasty loop */
  1231.             continue;
  1232.         }
  1233.         if(StatusLines || sp->split){
  1234.             if(c == 0x0a){
  1235.                 cputs(Eol);
  1236.                 clreol();
  1237.             } else if(c == 0x09)     /* TAB */
  1238.                 do putch(' '); while(wherex() % 8 != 1);
  1239.             else
  1240.                 putch(c);
  1241.         } else {
  1242.             putc(c,Rawterm);
  1243.         }
  1244.         /* Fix by Ron Murray, vk6zjm */
  1245.         if(sp->record != NULLFILE) {     /* Don't save CR if ascii mode */
  1246.             if(c == '\r' || c == '\n')
  1247.                 fflush(sp->record);
  1248.             if(c != '\r' || sockmode(sp->output, -1) != SOCK_ASCII)
  1249.                 putc(c,sp->record);
  1250.         }
  1251. #ifdef notdef
  1252.         if(sp->record != NULLFILE)
  1253.             putc(c,sp->record);
  1254. #endif
  1255.         if(sp->flowmode && c == '\n' && --sp->row <= 0){
  1256.             if(StatusLines)
  1257.                 cputs("--More--");
  1258.             else
  1259.                 fprintf(Rawterm,"--More--");
  1260.             sp->morewait = 1;
  1261.         }
  1262.     }
  1263. }
  1264.   
  1265. /* Return time since startup in milliseconds. If the system has an
  1266.  * 8254 clock chip (standard on ATs and up) then resolution is improved
  1267.  * below 55 ms (the clock tick interval) by reading back the instantaneous
  1268.  * value of the counter and combining it with the global clock tick counter.
  1269.  * Otherwise 55 ms resolution is provided.
  1270.  *
  1271.  * Reading the 8254 is a bit tricky since a tick could occur asynchronously
  1272.  * between the two reads. The tick counter is examined before and after the
  1273.  * hardware counter is read. If the tick counter changes, try again.
  1274.  * Note: the hardware counter counts down from 65536.
  1275.  */
  1276. int32
  1277. msclock()
  1278. {
  1279.     int32 hi;
  1280.     int16 lo;
  1281.     int16 count[4]; /* extended (48-bit) counter of timer clocks */
  1282.   
  1283.     if(!Isat)
  1284.         return Clock * MSPTICK;
  1285.   
  1286.     do {
  1287.         hi = Clock + Tick;
  1288.         lo = clockbits();
  1289.     } while(hi != Clock + Tick); /* Make sure a tick didn't just occur */
  1290.   
  1291.     count[0] = 0;
  1292.     count[1] = hi >> 16;
  1293.     count[2] = hi;
  1294.     count[3] = -lo;
  1295.     longmul(11,4,count);    /* The ratio 11/13125 is exact */
  1296.     longdiv(13125,4,count);
  1297.     return ((long)count[2] << 16) + count[3];
  1298. }
  1299. /* Return clock in seconds */
  1300. int32
  1301. secclock()
  1302. {
  1303.     int32 hi;
  1304.     int16 lo;
  1305.     int16 count[4]; /* extended (48-bit) counter of timer clocks */
  1306.   
  1307.     if(!Isat)
  1308.         return Clock * MSPTICK / 1000L;
  1309.   
  1310.     do {
  1311.         hi = Clock + Tick;
  1312.         lo = clockbits();
  1313.     } while(hi != Clock + Tick); /* Make sure a tick didn't just occur */
  1314.   
  1315.     count[0] = 0;
  1316.     count[1] = hi >> 16;
  1317.     count[2] = hi;
  1318.     count[3] = -lo;
  1319.     longmul(11,4,count);    /* The ratio 11/13125 is exact */
  1320.     longdiv(13125,4,count);
  1321.     longdiv(1000,4,count);
  1322.     return ((long)count[2] << 16) + count[3];
  1323. }
  1324.   
  1325. int
  1326. doisat(argc,argv,p)
  1327. int argc;
  1328. char *argv[];
  1329. void *p;
  1330. {
  1331.     return setbool(&Isat,"AT/386 mode",argc,argv);
  1332. }
  1333.   
  1334. /* Directly read BIOS count of time ticks. This is used instead of
  1335.  * calling biostime(0,0L). The latter calls BIOS INT 1A, AH=0,
  1336.  * wich resets the midnight overflow flag, losing days on the clock.
  1337.  */
  1338. long
  1339. bioscnt()
  1340. {
  1341.     if(Mtasker < 5) {   /* Read direct except under OS/2 or DPMI */
  1342.         int i_state;
  1343.         long rval;
  1344.   
  1345.         i_state = dirps();
  1346.         rval = * (long far *)MK_FP(0x40,0x6c);
  1347.         restore(i_state);
  1348.         return rval;
  1349.     } else {                /* Use BIOS call under OS/2 or DPMI */
  1350.         return(biostime(0,0L));
  1351.     }
  1352. }
  1353.   
  1354. /* same as getenv(), but return "" instead of NULL when it does not exist */
  1355. char *getnenv (name)
  1356. char *name;
  1357. {
  1358.     char *rv;
  1359.   
  1360.     if ((rv = getenv(name)) == NULL)
  1361.         rv = "";                       /* NULL replaced by "" */
  1362.   
  1363.     return rv;
  1364. }
  1365.   
  1366. #ifdef STATUSWIN
  1367.   
  1368. void StatusLine1(void);
  1369.   
  1370. /* First line. Global information */
  1371. void StatusLine1() {
  1372.   
  1373.     struct session *sp;
  1374.     int len=0,s,r,t;
  1375.     char *cp;
  1376.   
  1377.     extern int ConvUsers,ConvHosts,BbsUsers,FwdUsers,FtpUsers,SmtpUsers;
  1378. #ifdef MAILFOR    
  1379.     extern int Mail_Received;    /* see mailfor.c */
  1380.  
  1381.  
  1382. /* PE1DGZ: Show blinking 'MAIL' if unread mail is present */
  1383.     if(Mail_Received) {
  1384.         textattr(MainStColors | 0x80);
  1385.         len += cputs("MAIL ");
  1386.     }
  1387. #endif    
  1388.   
  1389.     /* Set the colors */
  1390.     textattr(MainStColors);
  1391.   
  1392. #ifdef MSDOS
  1393. #if __BORLANDC__ >= 0x0400
  1394.     {
  1395.         char buf[9];
  1396.   
  1397.         _strtime(buf);  /* This only exists in bc3.0 and up */
  1398.         buf[5] = ' ';
  1399.         buf[6] = '\0';
  1400.         len+=cputs(buf);
  1401.     }
  1402. #endif
  1403. #endif
  1404.   
  1405.     len+=cprintf("%5.5lu/%-6.6lu"
  1406. #ifdef CONVERS
  1407.     " CONV=%d LNKS=%d"
  1408. #endif
  1409. #ifdef MAILBOX
  1410.     " BBS=%d"
  1411. #ifdef MBFWD
  1412.     " FWD=%d"
  1413. #endif
  1414. #endif
  1415. #ifdef FTPSERVER
  1416.     " FTP=%d"
  1417. #endif
  1418. #ifdef SMTPSERVER
  1419.     " SMTP=%d"
  1420. #endif
  1421.     "  Ses:",
  1422.   
  1423.     Localheap(),coreleft()
  1424. #ifdef CONVERS
  1425.     ,ConvUsers, ConvHosts
  1426. #endif
  1427. #ifdef MAILBOX
  1428.     ,BbsUsers
  1429. #ifdef MBFWD
  1430.     ,FwdUsers
  1431. #endif
  1432. #endif
  1433. #ifdef FTPSERVER
  1434.     ,FtpUsers
  1435. #endif
  1436. #ifdef SMTPSERVER
  1437.     ,SmtpUsers
  1438. #endif
  1439.     );
  1440.   
  1441.     /* Print all active sessions . Modified from TNOS
  1442.      * Calculate how much room there is left on the line
  1443.      */
  1444.   
  1445.     for(sp=Sessions; sp < &Sessions[Nsessions];sp++) {
  1446. //        if(Numcols - len < 3)
  1447. //            break;
  1448.         if(sp->type == FREE || sp->type == COMMAND || sp->type == TRACESESSION)
  1449.             continue;
  1450.   
  1451.         /* if there is data waiting, blink the session number */
  1452.         r = socklen(sp->output,1);
  1453.         textattr ( (r) ? MainStColors | 0x80 : MainStColors);
  1454.         len+=cprintf (" %d", sp->num);
  1455.     }
  1456.     textattr(MainStColors); /* In case blinking was on! */
  1457.     clreol();
  1458. }
  1459.   
  1460. #ifdef MAILBOX
  1461. char *StBuf2;    /* allocated in main.c */
  1462. int StLen2;  
  1463.  
  1464. void StatusLine2(void);
  1465.   
  1466. void StatusLine2() {
  1467.     struct mbx *m;
  1468.     char *cp;
  1469.     int len;
  1470.   
  1471.     cp = StBuf2+StLen2;
  1472.     *cp = '\0';
  1473.     for(m=Mbox;m;m=m->next) {
  1474.         if((len = strlen(m->name)) != 0 && (len < Numcols-(cp-StBuf2)-4)) {
  1475.             *cp++ = ' ';
  1476.             if(m->sid & MBX_SID)
  1477.                 *cp++ = '*';    /* Indicate a bbs */
  1478.             else switch(m->state) {
  1479.                 case MBX_GATEWAY:
  1480.                     *cp++ = '!';
  1481.                     break;
  1482.                 case MBX_READ:
  1483.                 case MBX_SUBJ:
  1484.                 case MBX_DATA:
  1485.                     *cp++ = '#';
  1486.                     break;
  1487.                 case MBX_UPLOAD:
  1488.                 case MBX_DOWNLOAD:
  1489.                 case MBX_XMODEM_RX:
  1490.                 case MBX_XMODEM_TX:
  1491.                     *cp++ = '=';
  1492.                     break;
  1493.                 case MBX_SYSOPTRY:
  1494.                 case MBX_SYSOP:
  1495.                     *cp++ = '@';
  1496.                     break;
  1497.                 case MBX_CONVERS:
  1498.                 case MBX_CHAT:
  1499.                     *cp++ = '^';
  1500.                 case MBX_CMD:
  1501.                     *cp++ = ' ';    /* To keep things aligned nicely */
  1502.                     break;
  1503.                 default:
  1504.                     *cp++ = '?';
  1505.                     break;
  1506.             }
  1507.             strcpy(cp,m->name);
  1508.             cp += len;
  1509.         }
  1510.     }
  1511.     cputs(StBuf2);
  1512.     clreol();
  1513. }
  1514. #endif /* MAILBOX */
  1515.   
  1516. char *StBuf3;    /* allocated in main.c */
  1517. int StLen3;
  1518. void StatusLine3(void);
  1519.   
  1520. /* The session dependent data */
  1521. void StatusLine3() {
  1522.     char *cp;
  1523.     int  s,t,SesType;
  1524.     struct usock *up;
  1525.   
  1526.     static struct session *MyCurrent;
  1527.     static int SesNameLen;
  1528.     static int SockStatus,SockName,SesData;
  1529.   
  1530.     /* Set the colors */
  1531.     textattr(SesStColors);
  1532.   
  1533.     /* Next line. Session specific information */
  1534.     if(MyCurrent != Current) {
  1535.         /* Keep track of the current session */
  1536.         MyCurrent = Current;
  1537.         SesType = MyCurrent->type;
  1538.         /* Remember to offset for "\r\n" at start of buffer ! */
  1539.         SesNameLen = 2;
  1540.         SesNameLen+=sprintf(StBuf3+SesNameLen,"%d %s:",
  1541.         MyCurrent->num,Sestypes[SesType]);
  1542.   
  1543.         /* We can't show network socket data until socket is valid ! */
  1544.         SesData = SockName = 0;
  1545.   
  1546.         SockStatus = 1; /* Show socket status by default */
  1547.         if(SesType == COMMAND || SesType == TRACESESSION) {
  1548. //            if(Command->curdirs)
  1549. //                sprintf(StBuf3+SesNameLen, "%-30.30s",Command->curdirs->dir);
  1550.             SockStatus = 0;     /* Don't show socket name and ses data */
  1551.   
  1552.         } else if(SesType == MORE ||
  1553.             SesType == REPEAT ||
  1554.             SesType == LOOK) {
  1555.                 sprintf(StBuf3+SesNameLen," %-20.20s",MyCurrent->name);
  1556.                 SockStatus = 0;     /* Don't show socket name and ses data */
  1557.           }
  1558.     }
  1559.     /* Only if this is a session with a network socket do we show the status */
  1560.     if(SockStatus) {
  1561.         if(!SockName && (s=MyCurrent->s) != -1) {
  1562.             int i;
  1563.             struct sockaddr fsocket;
  1564.   
  1565.             /* The session now has a valid network socket.
  1566.              * Go get the name, and pointer.
  1567.              */
  1568.             if(getpeername(s,(char *)&fsocket,&i) == -1)
  1569.                 cp = "";
  1570.             else cp = psocket(&fsocket);
  1571.             SesNameLen+=sprintf(StBuf3+SesNameLen," %-18.18s TxQ ",cp);
  1572.             SockName = SesData = 1;
  1573.         }
  1574.         /* We have the socket name, now go print the socket session data */
  1575.         if(SockName) {
  1576.             /* Some sessions keep hanging on to their network socket
  1577.              * until the use hits return to close the session !
  1578.              * Others will delete it sooner...
  1579.              */
  1580.             if((s=MyCurrent->s) != -1 && ((cp=sockstate(s))!= NULL) ) {
  1581.                 /* Network socket for session still valid */
  1582.                 t = socklen(s,1);
  1583.                 StLen3 = SesNameLen +
  1584.                 sprintf(StBuf3+SesNameLen,"%4.4d St: %-12.12s",t,cp);
  1585.             } else {
  1586.                 SesData = 0;    /* Don't print rest of line 3 ! */
  1587.                 sprintf(StBuf3+SesNameLen-4,"  LIMBO !");
  1588.             }
  1589.             /* If the socket is still valid, print some data */
  1590.             if(SesData) {
  1591.                 up = itop(s);
  1592.                 switch(up->type) {
  1593.                     case(TYPE_TCP):
  1594.                     {
  1595.                         struct tcb *tcb = up->cb.tcb;
  1596.                         sprintf(StBuf3+StLen3,
  1597.                         " T: %5.5ld/%-5.5ld ms",
  1598.                         (long)read_timer(&tcb->timer),
  1599.                         (long)dur_timer(&tcb->timer));
  1600.                     }
  1601.                         break;
  1602. #ifdef AX25
  1603.                     case(TYPE_AX25I):
  1604.                     {
  1605.                         struct ax25_cb *axp = up->cb.ax25;
  1606.                         sprintf(StBuf3+StLen3,
  1607.                         " T1: %5.5ld/%5.5ld ms",
  1608.                         (long)read_timer(&axp->t1),
  1609.                         (long)dur_timer(&axp->t1));
  1610.                     }
  1611.                         break;
  1612. #endif
  1613. #ifdef NETROM
  1614.                     case(TYPE_NETROML4):
  1615.                     {
  1616.                         struct nr4cb *cb = up->cb.nr4;
  1617.                         sprintf(StBuf3+StLen3,
  1618.                         " T: %5.5ld/%5.5ld ms",
  1619.                         (long)read_timer(&cb->tcd),
  1620.                         (long)dur_timer(&cb->tcd));
  1621.                     }
  1622.                         break;
  1623. #endif
  1624.                 }
  1625.             }
  1626.         }
  1627.     }
  1628.     cputs(StBuf3);
  1629.     clreol();
  1630. }
  1631.   
  1632. /* Build the status window on the screen - WG7J */
  1633. void UpdateStatus() {
  1634.   
  1635.     if(!StatusLines)
  1636.         return;
  1637.   
  1638. #ifdef MULTITASK
  1639.     /* if we are shelled out with multi-task on, do not update the status */
  1640.     if(Background && Nokeys)
  1641.         return;
  1642. #endif
  1643.   
  1644.   
  1645.     {
  1646.         struct text_info ti;
  1647.   
  1648.     /* get the current output context */
  1649.         gettextinfo(&ti);
  1650.   
  1651.     /* create the window */
  1652.         window(1,1,Numcols,StatusLines);
  1653.   
  1654.         StatusLine1();  /* Global system status */
  1655.         if(StatusLines > 1)
  1656. #ifdef MAILBOX
  1657.             StatusLine2();  /* Mailbox user status */
  1658.         if(StatusLines > 2)
  1659. #endif
  1660.             StatusLine3();    /* Session dependent status */
  1661.   
  1662.     /* restore the previous output context */
  1663.         window((int)ti.winleft,(int)ti.wintop,
  1664.         (int)ti.winright,(int)ti.winbottom);
  1665.         textattr(ti.attribute);
  1666.         gotoxy(ti.curx,ti.cury);
  1667.     }
  1668. }
  1669.   
  1670. #endif /* STATUSWIN */
  1671.   
  1672.